import LXCOM from '../../common/common.js'
import abspkg from '../../graph/action/absaction.js'

import UtilFuncs from '../../common/tools/utilfuncs.js'
var funcs = UtilFuncs.UtilFuncs;

import {LGXEnums}  from '../../common/sysenum.js';


class EllipseArcAction extends abspkg.AbsGraphAction {
    constructor(graph) {
        super(graph);
        this.sureRadius = false;
        this.sureStPointFlag = false;
        this.prePt = { x:0, y:0 };
    }

    modifyByCMD(isNew, cmd) {
        var graph = this.getRefGraph();
        if (graph.getStart() == 1 && graph.getFinish() == 1) {
            return null;
        }

        let rst = null;
        var position = graph.getPos();
        let coordLst = cmd.split(",");
        cmd = cmd.toLowerCase();

        if (cmd == "e") {
            let finishFlag = false;
            let nextTips = "";
            if (graph.mouseDownTime == 2) {
                graph.mouseDownTime = 3;
                nextTips = "输入单个数字确定短轴半径值(r)或输入(x,y)或在图中点击确定半径。x,y,相对坐标使用:@x,y。使用当前位置（e）";
            }
            else if (graph.mouseDownTime == 3) {
                graph.mouseDownTime = 4;
                nextTips = "输入起始角度值。使用当前位置（e）";
            }
            // else if (graph.mouseDownTime == 2) {
            //     position.yRadius = parseFloat(rdesc);
            //     graph.mouseDownTime = 3;
            //     nextTips = "输入单个起始角度值。使用当前位置（e）";
            // }
            // else if (graph.mouseDownTime == 3) {
            //     position.stAngle = parseFloat(rdesc);
            //     graph.mouseDownTime = 4;
            //     nextTips = "输入单个终止角度值。使用当前位置（e）";
            // }
            // else if (graph.mouseDownTime == 4) {
            //     position.endAngle = parseFloat(rdesc);
            //     graph.mouseDownTime = 5;
            //     finishFlag = true;
            // }

            let cmd = { type: 'e', value: "" };
            let appendToPreCMD = "";
            rst = { cmdObj: cmd, nextTips, appendToPreCMD, finish: finishFlag };

            if (finishFlag){
                graph.setFinish(1);
                graph.mouseDownTime = 0;
            }
        }
        else if (coordLst.length == 1) {
            let rdesc = coordLst[0];
            if (isNew) {
                return null;
            }
            else if (graph.getStart() == 1) {
                let finishFlag = false;
                let nextTips = "";
                if (graph.mouseDownTime == 1) {
                    position.yRadius = parseFloat(rdesc);
                    position.xRadius = position.yRadius;
                    graph.mouseDownTime = 2;
                    nextTips = "输入单个数字确定短轴半径值(yR)或输入(x,y)或在图中点击确定半径。x,y,相对坐标使用:@x,y。使用当前位置（e）";
                }
                else if (graph.mouseDownTime == 2) {
                    position.yRadius = parseFloat(rdesc);
                    graph.mouseDownTime = 3;
                    nextTips = "输入起始角度值。使用当前位置（e）";
                }
                else if (graph.mouseDownTime == 3) {
                    position.stAngle = parseFloat(rdesc);
                    graph.mouseDownTime = 4;
                    nextTips = "输入终止角度值。使用当前位置（e）";
                }
                else if (graph.mouseDownTime == 4) {
                    position.endAngle = parseFloat(rdesc);
                    graph.mouseDownTime = 5;
                    finishFlag = true;
                }

                let cmd = { type: 'draw_lmousedown', value: rdesc };
                let appendToPreCMD = "";
                rst = { cmdObj: cmd, nextTips, appendToPreCMD, finish: finishFlag };
                if (finishFlag){
                    graph.setFinish(1);
                    graph.mouseDownTime = 0;
                }
            }
        }
        else if (coordLst.length == 2) {
            let xdesc = coordLst[0];
            let ydesc = coordLst[1];

            if (isNew) {
                if (xdesc.indexOf("@") >= 0) {
                    return null;
                }
                let x = parseFloat(xdesc);
                let y = parseFloat(ydesc);
                position.x = x;
                position.y = y;
                position.xRadius = 1;
                position.yRadius = 1;
                position.stAngle = 0;
                position.endAngle = 360;

                graph.mouseDownTime = 1;
                graph.setStart(1);

                let coordDecs = x.toFixed(2) + "," + y.toFixed(2);
                let cmd = { type: 'draw_lmousedown', value: coordDecs };
                let nextTips = "输入单个数字确定长轴半径值(xR)或输入(x,y)或在图中点击确定半径。x,y,相对坐标使用:@x,y。使用当前位置（e）";
                let appendToPreCMD = coordDecs;
                rst = { cmdObj: cmd, nextTips, appendToPreCMD, finish: false };
            }
            else if (graph.getStart() == 1) {
                let nextTips = "";

                let cnt = { x: position.x, y: position.y };
                let x = parseFloat(xdesc);
                let y = parseFloat(ydesc);
                let XRelateIdx = xdesc.indexOf("@");
                if (XRelateIdx >= 0) {
                    let tmp = xdesc.substr(XRelateIdx + 1, xdesc.length - XRelateIdx - 1);
                    x = cnt.x + parseFloat(tmp);
                    y = cnt.y + y;
                }
                let curentPt = { x, y };

                if (graph.mouseDownTime == 1) {
                    position.yRadius = funcs.distancePoint(curentPt, cnt, 1);
                    position.xRadius = position.yRadius;
                    graph.mouseDownTime = 2;
                    nextTips = "输入单个数字确定短轴半径值(yR)或输入(x,y)或在图中点击确定半径。x,y,相对坐标使用:@x,y。使用当前位置（e）";
                }
                else if (graph.mouseDownTime == 2) {
                    position.yRadius = funcs.distancePoint(curentPt, cnt, 1);
                    nextTips = "输入起始角度值。使用当前位置（e）";
                }

                let coordDecs = x.toFixed(2) + "," + y.toFixed(2);
                let cmd = { type: 'draw_lmousedown', value: coordDecs };
                
                let appendToPreCMD = "";
                rst = { cmdObj: cmd, nextTips, appendToPreCMD, finish: false };
            }
        }
        graph.calScope(1);
        return rst;
    }

    act2Mouse(mosEvnt, mousePt) {
        var graph = this.getRefGraph();
        if (graph == null) {
            return LGXEnums.LGXPlotState.ErrorState;
        }
        let state = LGXEnums.LGXPlotState.NoneState;
        if (graph.plotMode == LGXEnums.ArcPlotMode.CenterAngleMode) {
            state = this.act2MouseCenterAngleMode(mosEvnt, mousePt);
        }
        else if (graph.plotMode == LGXEnums.ArcPlotMode.Point3Mode) {
            state = this.act2Mouse3PointMode(mosEvnt, mousePt);
        }
        graph.calScope(1);
        return state;
    }

    act2MouseCenterAngleMode(mosEvnt, mousePt) {
        var graph = this.getRefGraph();
        var state = LGXEnums.LGXPlotState.NoneState;
        var position = graph.getPos();

        let testFlag = false;
        if (testFlag && mosEvnt.type == LGXEnums.LGXMouseMode.LMouseDown){
            position.x = 500;
            position.y = 500;
            position.xRadius = 100;
            position.yRadius = 80;
            position.stAngle = 90;
            position.endAngle = 180;
            graph.setStart(1);
            graph.setFinish(1);
            return LGXEnums.LGXPlotState.FinistState;
        }

        if (mosEvnt.type == LGXEnums.LGXMouseMode.LMouseDown) {
            if (graph.getStart() == 0) {
                graph.setStart(1);
                position.x = mousePt.x;
                position.y = mousePt.y;
                state = LGXEnums.LGXPlotState.OKState;
                graph.mouseDownTime = 1;
            }
            else if (graph.getStart() == 1 && graph.getFinish() == 0) {
                if (graph.mouseDownTime == 1) {
                    let dis = funcs.distancePoint(position, mousePt, 1);
                    if (!this.sureRadius) {
                        position.xRadius = dis;
                        position.yRadius = dis;
                    }
                    let vect = funcs.getVector(position, mousePt);
                    position.stAngle = vect.angleFrmXAxisPositiveDir();
                    graph.mouseDownTime = 2;
                    state = LGXEnums.LGXPlotState.OKState;
                }
                else if (graph.mouseDownTime == 2) {
                    let dis = funcs.distancePoint(position, mousePt, 1);
                    position.yRadius = dis;
                    let vect = funcs.getVector(position, mousePt);
                    position.endAngle = vect.angleFrmXAxisPositiveDir();
                    graph.mouseDownTime = 3;
                    state = LGXEnums.LGXPlotState.OKState;
                    graph.calControlPoint();
                }
                else  if (graph.mouseDownTime == 3) {
                    let vect = funcs.getVector(position, mousePt);
                    position.endAngle = vect.angleFrmXAxisPositiveDir();
                    graph.mouseDownTime = 4;
                    state = LGXEnums.LGXPlotState.FinistState;
                    graph.calControlPoint();
                    graph.setFinish(1);
                }
            }
        }
        else if (mosEvnt.type == LGXEnums.LGXMouseMode.mouseMove) {
            if (graph.getStart() == 1 && graph.getFinish() == 0) {
                if (graph.mouseDownTime == 1) {
                    graph.setDisAuxPoint(mousePt);
                }
                else if (graph.mouseDownTime == 2) {
                    let dis = funcs.distancePoint(position, mousePt, 1);
                    position.yRadius = dis;

                    let vect = funcs.getVector(position, mousePt);
                    position.endAngle = vect.angleFrmXAxisPositiveDir();
                }
                else if (graph.mouseDownTime == 3) {
                    let vect = funcs.getVector(position, mousePt);
                    position.endAngle = vect.angleFrmXAxisPositiveDir();
                }
            }

            state = LGXEnums.LGXPlotState.OKState;
        }
        return state;
    }

    act2Mouse3PointMode(mosEvnt, mousePt) {
        var graph = this.getRefGraph();
        var state = LGXEnums.LGXPlotState.NoneState;
        var position = graph.getPos();
        if (mosEvnt.type == LGXEnums.LGXMouseMode.LMouseDown) {
            if (graph.getStart() == 0) {
                graph.setStart(1);
                graph.setStPoint(mousePt);
                state = LGXEnums.LGXPlotState.AppendPtState;
                graph.mouseDownTime = 1;
            }
            else if (graph.getStart() == 1 && graph.getFinish() == 0) {
                if (graph.mouseDownTime == 1) {
                    graph.setMidPoint(mousePt);
                    graph.mouseDownTime = 2;
                    state = LGXEnums.LGXPlotState.OKState;
                }
                else if (graph.mouseDownTime == 2) {
                    graph.setEndPoint(mousePt);
                    graph.mouseDownTime = 3;
                    state = LGXEnums.LGXPlotState.FinistState;
                    graph.setFinish(1);
                    graph.calControlPoint();
                }
            }
        }
        else if (mosEvnt.type == LGXEnums.LGXMouseMode.mouseMove) {
            if (graph.getStart() == 1 && graph.getFinish() == 0) {
                if (graph.mouseDownTime == 1) {
                    graph.setMidPoint(mousePt);
                    state = LGXEnums.LGXPlotState.OKState;
                }
                else if (graph.mouseDownTime == 2) {
                    graph.setEndPoint(mousePt);
                    graph.calControlPoint();
                }
            }
            state = LGXEnums.LGXPlotState.OKState;
        }
        graph.calScope(1);
        return state;
    }
}

export default { EllipseArcAction }
